package top.csy8888.ch04;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @author CSY
 * @date 2025 - 10 - 10
 */
public class Producer {
    private static final String EXCHANGE_NAME = "my.fanout";

    private static final String ROUTER_KEY = "";

    private static final ConnectionFactory connectionFactory;

    static {
        connectionFactory = new ConnectionFactory();

        connectionFactory.setHost("127.0.0.1");

        connectionFactory.setPort(5672);

        connectionFactory.setUsername("csy");

        connectionFactory.setPassword("csy11348336");
    }

    public static void send(String message) {
        try(Connection conn = connectionFactory.newConnection();
        Channel channel = conn.createChannel()) {
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);

            channel.basicPublish(EXCHANGE_NAME, ROUTER_KEY, null, message.getBytes());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        send("Hello World!");
    }
}
